﻿using System;
using System.Collections.Generic;

using Fuse8.DomainFramework.Common;
using Fuse8.DomainFramework.Common.Locator;

using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Domain.Commands;
using Fuse8.ContentManagementFramework.Domain.Commands.CommandParameters;
using Fuse8.ContentManagementFramework.Domain.Special;

namespace Fuse8.ContentManagementFramework.BizFacade.Services
{    
    public class CmsContentService : CmsSiteBusinessService<CmsContent>
    {
        private CmsContentContainerService _contentContainerService;

        protected virtual CmsContentContainerService ContentContainerService
        {
            get
            {
                if (_contentContainerService == null)
                {
                    _contentContainerService = new CmsContentContainerService();
                }

                return _contentContainerService;
            }
        }

        public virtual CmsContentContainer FindForContentType(CmsContentType contentType, int currentView, int viewCapacity)
        {
            Guard.ArgumentNotNull(contentType, "contentType");

            return ContentContainerService.FindForContentType(contentType.Id, currentView, viewCapacity);
        }

        public virtual CmsContentContainer FindForContentType(int contentTypeId, int currentView, int viewCapacity)
        {
            return ContentContainerService.FindForContentType(contentTypeId, currentView, viewCapacity);
        }
    }

    public class CmsContentContainerService : CmsSiteBusinessService<CmsContentContainer>
    {
        public virtual CmsContentContainer FindForContentType(CmsContentType contentType, int currentView, int viewCapacity)
        {
            Guard.ArgumentNotNull(contentType, "contentType");

            return FindForContentType(contentType.Id, currentView, viewCapacity);
        }

        public virtual CmsContentContainer FindForContentType(int contentTypeId, int currentView, int viewCapacity)
        {
            var command = DependencyServiceLocator.Current.GetInstance<CmsContentGetForContentTypeCommand>();

            var commandParameter = new CmsContentGetForContentTypeCommandParameter
            {
                Id = contentTypeId,
                CurrentView = currentView,
                TotalOnTheView = viewCapacity
            };

            SiteAwareEntityObjectMediator.SetSiteEntity(commandParameter);

            command.ConfigureCommand(commandParameter);

            return FindOne(command);
        }
    }
}
